package com.Dialogs;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner;
import android.widget.TextView;
import com.Adapters.SpinnerAdapter;
import com.Managers.UserManagerClass;
import com.TextFormat.TagEditText;
import com.TextFormat.UtilFonts;
import com.shareup.R;
import com.utils.Utils;


public class DialogManagerClass {

    private static Button button;

    private final static OnItemSelectedListener ClickPrivacy = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapt, View arg1, int pos, long arg3) {

            String item = data.get(pos).get("id");
            privacyIndex = Integer.parseInt(item);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

            // TODO Auto-generated method stub

        }
    };

    private static TextView location;

    private static CheckBox locationCheck;

    private static TagEditText mu;

    private static int privacyIndex;

    static ArrayList<Map<String, String>> data;

    static String tagString;

    public static void showAddAlbumDialog(Context context, final onNewAlbumNamed callback) {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_add_album, null);
        // ///////////////////////////////////////////////////////////////////////////////////////////////////////
        button = (Button) v.findViewById(R.id.dialog_tag_OK);
        final EditText names = (EditText) v.findViewById(R.id.commentext);
        final EditText pin = (EditText) v.findViewById(R.id.album_pin);
        final Spinner spinner = (Spinner) v.findViewById(R.id.sp_shared);
        TextView tx = (TextView) v.findViewById(R.id.sharewith_text);
        addPrivacy(spinner);
        // /////////////////////////////////////////////////////////////////////////
        UtilFonts.setTextRegularFont(names, pin, button, tx);

        // //////////////////////////////////////////////////////////////////
        dg.setView(v);
        // /////////////////////////////////////////////

        // ///////////////////////////////////////////////////////
        final AlertDialog alert = dg.create();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean errors = false;
                int pins = 0;
                String textp = pin.getText().toString();
                if (textp.trim().length() > 0) {
                    if (Utils.isNumeric(textp)
                                && textp.trim().length() > 3) {
                        pins = Integer.parseInt(textp);
                        errors = false;
                    }
                    else {
                        pin.setError(v.getContext().getString(R.string.error_pin_create));
                        errors = true;
                    }
                }

                int sel = spinner.getSelectedItemPosition();
                String nam = names.getText().toString();

                if (nam.trim().length() < 4) {
                    names.setError(v.getContext().getString(R.string.error_field_username_short));
                    errors = true;
                }
                else
                    errors = false;

                if (!errors) {
                    callback.Named(nam, pins, sel);
                    alert.dismiss();
                }

            }
        });

        alert.show();

    }

    public static void showAddAlbumDialog(Context context, String hint, final onNewAlbumNamed callback) {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_add_album, null);
        // ///////////////////////////////////////////////////////////////////////////////////////////////////////
        button = (Button) v.findViewById(R.id.dialog_tag_OK);
        final EditText comment = (EditText) v.findViewById(R.id.commentext);

        // /////////////////////////////////////////////////////////////////////////
        UtilFonts.setTextRegularFont(comment, button);

        // //////////////////////////////////////////////////////////////////
        dg.setView(v);
        // /////////////////////////////////////////////

        // ///////////////////////////////////////////////////////
        final AlertDialog alert = dg.create();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // callback.Named(comment.getText().toString());
                alert.dismiss();

            }
        });

        alert.show();

    }

    public static void showAddCommentDialog(Context context, String commentString, final onCommentChange callback) {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_add_comment, null);
        // ///////////////////////////////////////////////////////////////////////////////////////////////////////
        button = (Button) v.findViewById(R.id.dialog_tag_OK);
        final EditText comment = (EditText) v.findViewById(R.id.commentext);

        comment.setText(commentString);
        comment.setSelection(commentString.length());
        // /////////////////////////////////////////////////////////////////////////
        UtilFonts.setTextRegularFont(comment);
        UtilFonts.setTextRegularFont(button);
        // //////////////////////////////////////////////////////////////////
        dg.setView(v);
        // /////////////////////////////////////////////
        dg.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface arg0) {

                callback.Changed(comment.getText().toString());
            }
        });
        // ///////////////////////////////////////////////////////
        final AlertDialog alert = dg.create();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                callback.Changed(comment.getText().toString());
                alert.dismiss();

            }
        });

        alert.show();

    }

    public static void showAddTagDialog(Context context, String tagString, final onTagChange callback) {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_add_tags, null);
        // ///////////////////////////////////////////////////////////////////////////////////////////////////////
        button = (Button) v.findViewById(R.id.dialog_tag_OK);
        mu = (TagEditText) v.findViewById(R.id.tagtext);
        //
        if (!tagString.isEmpty())
            mu.setText(tagString);
        //
        mu.setDropDownBackgroundResource(R.drawable.background_tags_selection);
        mu.setAdapter(new ArrayAdapter<String>(context, R.layout.item_dropdown));
        mu.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        // /////////////////////////////////////////////////////////////////////////
        UtilFonts.setTextRegularFont(mu);
        UtilFonts.setTextRegularFont(button);
        // //////////////////////////////////////////////////////////////////
        dg.setView(v);
        if ((tagString.length() > 1))
            UtilFonts.setChips(mu);
        // /////////////////////////////////////////////
        dg.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface arg0) {

                callback.Changed(mu.getText().toString().trim());
            }
        });
        // ///////////////////////////////////////////////////////
        final AlertDialog alert = dg.create();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                callback.Changed(mu.getText().toString().trim());
                alert.cancel();
            }
        });
        //
        alert.show();
        //
    }

    public static void showShareLocationDialog(Context context) {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_add_location, null);
        // ///////////////////////////////////////////////////////////////////////////////////////////////////////
        button = (Button) v.findViewById(R.id.acrop_dialog_OK);
        location = (TextView) v.findViewById(R.id.acrop_location);
        locationCheck = (CheckBox) v.findViewById(R.id.acrop_check_location);
        locationCheck.setChecked(UserManagerClass.UserLogged.isActiveLocation());
        locationCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean checked) {

                if (UserManagerClass.UserLogged != null) {
                    UserManagerClass ul = new UserManagerClass(view.getContext());
                    UserManagerClass.UserLogged.setActiveLocation(checked);

                    ul.saveActiveLocation(UserManagerClass.UserLogged);
                }
                else {
                    location.setText("ERROR WITH USER");
                }
            }
        });
        //
        if (UserManagerClass.UserLocation != null
                    && UserManagerClass.UserLocation.getAddress() != null) {
            location.setText(String.format(context.getString(R.string.crop_location), UserManagerClass.UserLocation.toString()));
        }
        else {
            location.setText(context.getString(R.string.error_getting_location));
            locationCheck.setChecked(false);
            locationCheck.setEnabled(false);
            UserManagerClass.UserLogged.setActiveLocation(false);
        }
        // /////////////////////////////////////////////////////////////////////////
        UtilFonts.setTextRegularFont(location);
        UtilFonts.setTextRegularFont(locationCheck);
        UtilFonts.setTextRegularFont(button);
        // //////////////////////////////////////////////////////////////////
        dg.setView(v);
        // /////////////////////////////////////////////
        final AlertDialog alert = dg.create();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                alert.dismiss();
            }
        });
        //
        alert.show();
        //
    }

    private static void addPrivacy(Spinner sharewith) {

        String[] array = sharewith.getContext().getResources().getStringArray(R.array.picture_shared_with);
        int cont = 0;
        data = new ArrayList<Map<String, String>>();
        for (String s : array) {
            Map<String, String> datum = new HashMap<String, String>(2);
            datum.put("id", String.valueOf(cont));
            datum.put("name", s);
            data.add(datum);
            cont++;
        }
        SpinnerAdapter adapter = new SpinnerAdapter(sharewith.getContext(), data, R.layout.item_dropdown_spinner, new String[] {
                    "name", "id" }, new int[] { android.R.id.text1 });

        sharewith.setAdapter(adapter);
        sharewith.setOnItemSelectedListener(ClickPrivacy);
        sharewith.setSelection(1);

    }

    public interface onCommentChange {

        void Changed(String text);
    }

    public interface onNewAlbumNamed {

        void Named(String name, int pin, int privacy);
    }

    public interface onTagChange {

        void Changed(String text);
    }
}
